package com.moyq5.permit.center.web.jackson.filter;

import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.moyq5.mvc.framework.utils.StringUtils;
import com.moyq5.permit.center.bean.entity.Shop;
import com.moyq5.permit.center.bean.entity.ShopAdmin;
import com.moyq5.permit.center.bean.entity.SysAdmin;

/**
 * @author Moyq5's MvcMaker Tools
 */
public abstract class ShopAdminFilter {

	public static FilterProvider get(String filter) {
		if (StringUtils.isEmpty(filter)) {
			return get();
		}
		try {
			Method method = ShopAdminFilter.class.getMethod("getWith" + String.valueOf(filter.charAt(0)).toUpperCase() + filter.substring(1));
			return (FilterProvider)method.invoke(null);
		} catch (Exception e) {
			return get();
		}
	}
	
	public static Set<String> baseFields() {
		final Set<String> shopAdmin = new HashSet<String>();
		shopAdmin.add("id");
		shopAdmin.add("roles");
		return shopAdmin;
	}

	public static FilterProvider getId() {
		final Set<String> shopAdmin = new HashSet<String>();
		shopAdmin.add("id");
		
		return new SimpleFilterProvider().addFilter(
				ShopAdmin.class.getSimpleName(),
				SimpleBeanPropertyFilter.filterOutAllExcept(shopAdmin));
	}

	public static FilterProvider get() {
		return new SimpleFilterProvider().addFilter(
				ShopAdmin.class.getSimpleName(),
				SimpleBeanPropertyFilter.filterOutAllExcept(baseFields()));
	}
	
	public static FilterProvider getWithAdmin() {
		final Set<String> shopAdmin = baseFields();
		shopAdmin.add("admin");
		final Set<String> admin = SysAdminFilter.baseFields();

		return new SimpleFilterProvider()
				.addFilter(ShopAdmin.class.getSimpleName(), SimpleBeanPropertyFilter.filterOutAllExcept(shopAdmin))
				.addFilter(SysAdmin.class.getSimpleName(), SimpleBeanPropertyFilter.filterOutAllExcept(admin));
	}

	public static FilterProvider getWithShopAndAdmin() {
		final Set<String> shopAdmin = baseFields();
		shopAdmin.add("shop");
		shopAdmin.add("admin");
		final Set<String> admin = SysAdminFilter.baseFields();
		final Set<String> shop = ShopFilter.baseFields();

		return new SimpleFilterProvider()
				.addFilter(ShopAdmin.class.getSimpleName(), SimpleBeanPropertyFilter.filterOutAllExcept(shopAdmin))
				.addFilter(Shop.class.getSimpleName(), SimpleBeanPropertyFilter.filterOutAllExcept(shop))
				.addFilter(SysAdmin.class.getSimpleName(), SimpleBeanPropertyFilter.filterOutAllExcept(admin));
	}

}
